<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *项目管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('start','off');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择项目！','error');
	}
}
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$category=pdo_getall('jy_sort_item_type',$params,'id');
	if(empty($category)){
			$inserts=$this->itemtypes();
			foreach($inserts as $k=>$insert){
				$insert['type']=$insert['id'];
				unset($insert['id']);
				$insert['uniacid']=$_SESSION['uniacid'];
				pdo_insert('jy_sort_item_type',$insert);
			}
	}
	
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title','status','star','type','logo','price');
	$list=pdo_getslice('jy_sort_item_type',$params,array($pindex,$psize),$total,$fields,'','star desc, id desc');
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('jy_sort/list//type='.$item['id'],true);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'项目列表'),
			array('op'=>'edit','title'=>'添加项目'),
		),
		'list'=>array('data'=>'list','delall'=>false,'list'=>array(
			array('name'=>'star','title'=>'排序','type'=>'string'),
			array('name'=>'title','title'=>'项目名称','type'=>'string'),
			array('name'=>'logo','title'=>'项目logo','type'=>'image'),
			array('name'=>'type','title'=>'项目模型','type'=>'select','options'=>'itemtypes'),
		//	array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'status','title'=>'是否启用','type'=>'switch','set'=>'set'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
		),'action'=>array(
			array('type'=>'href','action'=>'item_list','title'=>'信息列表'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'项目名称','type'=>'string','width'=>'15'),
			array('name'=>'status','title'=>'是否启用','type'=>'select','width'=>'15','options'=>'select_status'),
		)
	);
	$return['itemtypes']=$this->itemtypes();
	$return['select_status']=array(array('id'=>0,'title'=>'禁用'),array('id'=>1,'title'=>'启用'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return['itemtypes']=$this->itemtypes();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_sort_item_type',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	
	$return['item']=$item;
	$return['item']['diyfileds']=json_decode($return['item']['diyfileds'],true);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回项目列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'项目名称','name'=>'title','help'=>''),
					array('type'=>'image','title'=>'项目logo','name'=>'logo','help'=>''),
					array('type'=>'string','title'=>'项目备注','name'=>'remark','help'=>''),
					array('type'=>'select','title'=>'项目模型','name'=>'type','options'=>'itemtypes','help'=>'','show'=>array('isdiy'=>0),),
					array('type'=>'switch','title'=>'是否自定义模型','name'=>'isdiy','help'=>'自定义分类信息发布模型,下面会显示需要填写的表单项！(默认包含：标题,说明,图片,联系人,电话,地址等)'),
					array(
							'type'=>'array',
							'name'=>'diyfileds',
							'show'=>array('isdiy'=>1),
							'add_tip'=>'添加自定义表单项',
								'list'=>array(
									array('type'=>'string','name'=>'title','title'=>'标题','help'=>''),
									array('type'=>'radio','name'=>'type','title'=>'类型','options'=>'types'),
									array('type'=>'string','name'=>'placeholder','title'=>'提示语'),
									array('type'=>'radio','name'=>'ismust','title'=>'*必填?','options'=>'statuss'),
								),
								'details'=>array(
									array('type'=>'string','name'=>'title','title'=>'标题','help'=>''),
									array('type'=>'radio','name'=>'type','title'=>'类型','options'=>'types'),
									array(
											'type'=>'array_simple',
											'show'=>array('type'=>array(4,5)),
											'name'=>'value',
											'add_tip'=>'添加自定义表单项',
												'fields'=>array(
													array('type'=>'string','name'=>'title','title'=>'选项名称','help'=>''),
												),
									),
									array('type'=>'string','name'=>'placeholder','title'=>'提示语'),
									array('type'=>'switch','name'=>'ismust','title'=>'*必填?'),
								)
					),
					array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>'设置是否启用'),
					array('name'=>'star','title'=>'排序','type'=>'string'),
				)
			),
			
		)
	);
	$return['types']=get_form_types();
	$return['statuss']=array('否','是');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'logo'=>trim($postdata['logo']),
		'price'=>intval($postdata['price']),
		'remark'=>trim($postdata['remark']),
		'diyfileds'=>ijson_encode($postdata['diyfileds']),
		'isdiy'=>intval($postdata['isdiy']),
		'type'=>intval($postdata['type']),
		'status'=>intval($postdata['status']),
		'star'=>intval($postdata['star']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_sort_item_type',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$istype=pdo_getcolumn('jy_sort_item_type',array('type'=>intval($postdata['type']),'uniacid'=>$_SESSION['uniacid']),'id');
		if($istype){
				//exi('该模型的已经添加过项目了！','error');
		}
		pdo_insert('jy_sort_item_type',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_sort_item_type',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='star'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['star'])){
		$update['star']=$post['star'];
	}
	if($update){
		pdo_update('jy_sort_item_type',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的项目！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_sort_item_type',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['issh'])){
		pdo_update('jy_sort_item_type',array('isshow'=>intval($post['issh'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
